package com.example.test.controllers;

import com.example.test.dao.DirectorRepository;
import com.example.test.dao.MovieRepository;
import com.example.test.entities.Director;
import com.example.test.entities.Movies;
import com.example.test.services.DirectorService;
import com.example.test.services.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("directors")
public class DirectorControllers {

    @Autowired
    DirectorService directorService;
    @Autowired
    MovieService movieService;

    @GetMapping
    public  String displayStudents(Model model){
        List<Director> directors = directorService.getAll();
        model.addAttribute("directors",directors);
        return "directors/list-directors";
    }

    @GetMapping("/new")
    public String displayDirectorForm(Model model){

        Director aDir=new Director();
        List<Movies> movies = movieService.getAll();
        model.addAttribute("director",aDir);
        model.addAttribute("thisMovie",movies);
        return "new-director";
    }

    @PostMapping("/save")
    public String creatDirector(Model model,@RequestParam List<Long> movies, Director director){
        directorService.save(director);
        return "redirect:/directors";
    }

}